package com.trg.flex.frm
{
	import flash.display.DisplayObject;
	
	import mx.controls.Text;
	import mx.events.FlexEvent;

	public class FormTextOutput extends Text implements FormElement
	{
		private var formContainer:FormContainer;
		private var topContainer:DisplayObject;
		private var _property:String;
		
		public function FormTextOutput()
		{
			super();
			topContainer = this;
			topContainer.addEventListener(FlexEvent.ADD, addListener);
		}
		
		private function addListener(event:FlexEvent):void {
			topContainer.removeEventListener(FlexEvent.ADD, addListener);
			
			while(true) {
				if (topContainer.parent is FormContainer) {
					formContainer = topContainer.parent as FormContainer;
					formContainer.addElement(this);
					break;
				} else if (topContainer.parent == null) {
					topContainer.addEventListener(FlexEvent.ADD, addListener);
					break;
				}
				topContainer = topContainer.parent;
			}
		}
		
		public function get elementValue():Object
		{
			return text;
		}
		
		public function set elementValue(value:Object):void
		{
			if (value == null || value is String) {
				text = value as String;
			} else {
				text = value.toString();
			}
		}
		
		public function get property():String
		{
			return _property;
		}
		
		public function set property(property:String):void
		{
			_property = property;
		}
		
	}
}